package com.itheima.web;

import com.itheima.pojo.User;
import com.itheima.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求乱码
        request.setCharacterEncoding("utf-8");
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //创建User对象
        User user = new User();
        //将请求参数封装到User对象中
        user.setPassword(password);
        user.setUsername(username);
        //创建service业务层对象
        UserServiceImpl userService = new UserServiceImpl();
        //使用业务层对象调用业务层的登录方法将User对象传递到业务层
        User u = userService.login(user);
        //判断返回值是否等于null
        if (u != null) {
            response.getWriter().print(true);
        }else{
            response.getWriter().print(false);
        }
    }
}
